﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ZX.SYS.Bussiness;
using ZX.SYS.Library;
using Newtonsoft.Json;
namespace ZX.WebApp.Message
{
    /// <summary>
    /// MQMessage 的摘要说明
    /// </summary>
    public class MQMessage : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string type = context.Request["TYPE"];
            string para = context.Request["PARA"];
            context.Response.Write(Proc(type, para));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private string Proc(string type, string para)
        {
            string back = "";
            switch (type)
            {
                case "GetMQOrgType":
                    back = GetMQOrgType(para);
                    break;
                case "InsertUser":
                    back = InsertUser(para);
                    break;
                case "MegReback":
                    back = MegReback(para);
                    break;
                case "OrgbodyGroup":
                    back = OrgbodyGroup(para);
                    break;
                case "DeleteFromType":
                    back = DeleteFromType(para);
                    break;
            }
            return back;
        }
        private string GetMQOrgType(string para)
        {
            MQproccess btth = new MQproccess();
            return JsonConvert.SerializeObject(btth.GetMQOrgType(para));
        }

        private string DeleteFromType(string para)
        {
            string[] ps = para.Split('︾');
            MQproccess mp = new MQproccess();
            int b=mp.DeleteFromType(ps[0].TrimEnd(';'), ps[1]);
            if ( b>= 1)
            {
                return "成功移除"+b.ToString()+"条记录！";
            }
            else
            {
                return "移除失败";
            }
        }
        private string InsertUser(string para)
        {
            //return para;
            string view = "";
            string[] ps = para.Split('︾');
            if (ps[0] == "0")
                view = "V_UserMessage";
            else
                view = "V_CrmCustomerShowM";

            MQproccess mqp = new MQproccess();
            return mqp.InsertUser(view,ps[1],ps[2],ps[3]);
        }

        //用户短信答复
        private string MegReback(string para)
        {
            //ids + "︾" + jis + "︾" + contente+ "︾" + userid
            string back = "";
            string[] ps = para.Split('︾');
            MQproccess mqp = new MQproccess();
            if (ps[0].Contains(";"))
            {
                string[] ids = ps[0].TrimEnd(';').Split(';');
                string[] jis = ps[1].TrimEnd(';').Split(';');
                for (int i = 0; i < ids.Length; i++)
                { 
                    string[] phs=jis[i].Split('>');
                    if (mqp.sendmessage(phs[1], ps[2]) == "0")
                    {
                        if (mqp.UpdateReback(ids[i], ps[2], DateTime.Now, phs[1], 1, new Guid(ps[3])) == 1)
                        {
                            back += jis[i] + ":发送成功！";
                        }
                        else
                        {
                            back += jis[i] + ":短信发送成功,保存到数据库失败！";
                        }
                    }
                    else
                    {
                        back += jis[i] + ":发送失败！";
                    }
                
                }
                
            }
            else
            {
                string[] phs = ps[1].Split('>');
                if (mqp.sendmessage(phs[1], ps[2]) == "0")
                {
                    if (mqp.UpdateReback(ps[0].TrimEnd(';'), ps[2], DateTime.Now, phs[1], 1, new Guid(ps[3])) == 1)
                    {
                        back += ps[1] + ":发送成功！";
                    }
                    else
                    {
                        back += ps[1] + ":短信发送成功,保存到数据库失败！";
                    }
                }
                else
                {
                    back += ps[1] + ":发送失败！";
                }
            
            }
            return back;
            
        }

        private string OrgbodyGroup(string para)
        {
            string[] ps = para.Split('︾');
            MQproccess mp = new MQproccess();
            return mp.InserGroupType(ps[0],ps[1]);
        }
    }
}